package cn.com.surker.task;

import cn.com.surker.cache.MemoryCache;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.production.indent.ProTypeEnum;
import cn.com.surker.handler.DisCoordinator;
import cn.com.surker.production.entity.ProductionIndent;
import cn.com.surker.production.entity.vo.InvoiceHandleDetail;
import cn.com.surker.production.entity.vo.ProductionInvoiceVo;
import cn.com.surker.production.entity.vo.TranCarVo;
import cn.com.surker.production.service.ErpIndentService;
import cn.com.surker.util.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.*;

/**
 * @author luotao
 * @date 2025/4/2
 */
@Slf4j
@Configuration
@EnableScheduling
public class ProDisTask {


	@Autowired
	private ErpIndentService indentService;

	@Autowired
	private DisCoordinator disCoordinator;

	private MemoryCache memoryCache = MemoryCache.getInstance();

	/**
	 * 自动供比两天前的任务单
	 */
	@Scheduled(cron = "34 53 23 * * ?")
	public void closeIndent() {
		Date nowDate = new Date();
		//	提前两天的数据
		Date date = DateUtil.convertBeforeDayDate(nowDate, 2);
		//	更新任务单数据
		UpdateWrapper<ProductionIndent> uw = new UpdateWrapper<>();
		uw.eq("status", StateEnum.ENABLED.getCode());
		//	筛选条件为2天前正供或者代供的
		uw.and(e -> {
			e.in("type", ProTypeEnum.WAIT.getCode(), ProTypeEnum.PAUSE_PRO.getCode(), ProTypeEnum.WORK.getCode()).or().isNull("type");
		});
		uw.lt("actual_start_time", date);
		//	更新数据为供毕
		uw.set("type", ProTypeEnum.END.getCode());
		indentService.update(uw);
	}


	/**
	 * 生产线的正在生产的信息,2秒中执行一次
	 */
	@Scheduled(cron = "0/2 * * * * ? ")
	public void lingProducing() {
		ArrayList<Map<String, Object>> resList = disCoordinator.lingProducing();
		memoryCache.set(DisCoordinator.lingProducingKey, resList);
	}

	/**
	 * 查询全部车辆
	 */
	@Scheduled(cron = "0/10 * * * * ? ")
	public void allCarList() {
		List<TranCarVo> tranCars = disCoordinator.carList();
		if (tranCars == null) {
			return;
		}
		ArrayList cacheList = new ArrayList(tranCars);
		memoryCache.set(DisCoordinator.allCarKey, cacheList);
	}

	/**
	 * 查询退砼车辆
	 */
	@Scheduled(cron = "0/10 * * * * ? ")
	public void backInvList() {
		List<InvoiceHandleDetail> handleDetails = disCoordinator.nearlyBackList();
		if (handleDetails == null) {
			return;
		}
		ArrayList cacheList = new ArrayList(handleDetails);
		memoryCache.set(DisCoordinator.backInvKey, cacheList);
	}

	@Scheduled(cron = "0/5 * * * * ? ")
	public void disInvList() {
		List<ProductionInvoiceVo> invoiceVoList = disCoordinator.disInvList();
		if (invoiceVoList == null) {
			return;
		}
		ArrayList cacheList = new ArrayList(invoiceVoList);
		memoryCache.set(DisCoordinator.disInvKey, cacheList);
	}

	/**
	 * 查询任务单的累计车次和累计方量,30秒1次
	 */
	@Scheduled(cron = "0/30 * * * * ? ")
	public void accuAndTran() {
		//	查询当前正供的任务单信息
		Object indentIdList = memoryCache.get(DisCoordinator.workIndentKey);
		if (indentIdList != null) {
			List<Long> workIndentIdList = (List<Long>) indentIdList;
			//	循环查询任务单的累计车次和数量
			for (int i = 0, s = workIndentIdList.size(); i < s; i++) {
				Long indentId = workIndentIdList.get(i);
				//	查询信息
				HashMap<String, Object> map = disCoordinator.accuAndTran(indentId);
				//	存入缓存
				String accuAndTranKey = DisCoordinator.accuAndTranKey + "_" + indentId;
				// 存入缓存,7200秒后作废
				memoryCache.set(accuAndTranKey, map, 7200);
			}
		}
	}


	/**
	 * 查询近10车
	 */
	@Scheduled(cron = "0/30 * * * * ? ")
	public void nearlyInvList() {
		List<ProductionInvoiceVo> invoiceVoList = disCoordinator.nearlyInvList();
		if (invoiceVoList == null) {
			return;
		}
		ArrayList cacheList = new ArrayList(invoiceVoList);
		memoryCache.set(DisCoordinator.nearlyInvKey, cacheList);
	}

}
